/*
========================================================================
 Name        : SingaporeBusServicesListBoxView.h
 Author      : 
 Copyright   : Your copyright notice
 Description : 
========================================================================
*/
#ifndef __SMRTQUERY__
#define __SMRTQUERY__


#include "HTTPUtils.h"
#include "CActiveHelper.h"
#include <euserhl.h>
#include "ActiveTarget.h"
#include "imageconversion.h"
#include <http.h>
#include <bitmaptransforms.h>
#include <akndialog.h>
#include "ArrivalTimeQuery.h"
#include <es_sock.h>

class CSmrtSession;

struct CSmrtQuery : public CArrivalTimeQuery
{
	CONSTRUCTORS_MAY_LEAVE
	typedef scalp::CFunction<void(CSmrtQuery*, const TDesC8&, const TDesC8&, TServiceRequestStatus, const TDesC&, const TDesC8&, const TDesC8&)> TCallback;
	HBufC8* iServiceName;
	HBufC8* iStationNumber;
	TServiceRequestStatus iRequestStatus;
	RHTTPTransaction iServiceTransaction;	
	CHTTPTransactionTarget* iServiceTransactionTarget;
	CHTTPTransactionSource* iServiceTransactionSource;
	LManagedPtr< TCallback > iCallback;
	CSmrtSession* iSession;
	enum { ESmrtConnect, ESmrtArrivalTime } iTransactionType;
	CSmrtQuery(CSmrtSession* aSession, const TDesC8& aServiceName, const TDesC8& aStationNumber, TCallback* aCallback);
	void SubmitL();
	void Cancel();
	~CSmrtQuery();
};


	
class CSmrtSession : public CBase, public MActiveHelperCallback<RTimer*>
	{
	
RArray<CSmrtQuery*> iSmrtQueries;
friend class CSmrtQuery;
public:
	// constructors and destructor
	CSmrtSession();
	static CSmrtSession* NewL();
	static CSmrtSession* NewLC();        
	void ConstructL();
	virtual ~CSmrtSession();
	
	TTime iLastFailureSmrtConnect;
	TTime iLastFailureCrowdSmrtSession;
	void SubmitSmrtConnectRequestL(CSmrtQuery* aQuery);
	void ReceivedSmrtConnectAnswerL(CSmrtQuery* aQuery, RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf);
	TInt SmrtConnectTransactionError(CSmrtQuery* aQuery, TInt aError, RHTTPTransaction aTransaction);

	LManagedPtr<CDeflateDecompressor> iDeflateDecompressor;

	RHTTPSession iHTTPSession;
	void SetConnectionInfoL(const RConnection& aConnection, const RSocketServ& aSocketServ);
	void CloseConnectionL();

	RTimer iSmrtTimeoutTimer, iCrowdSmrtTimeoutTimer,iSmrtConnectTimeoutTimer;
	CActiveHelper<RTimer*>* iSmrtTimeoutTimerHelper, * iCrowdSmrtTimeoutTimerHelper;
	void MActiveHelperCallbackRunL(CActiveHelper<RTimer*>* aActiveHelper, RTimer* aTimer);
	TInt MActiveHelperCallbackRunError(CActiveHelper<RTimer*>* aActiveHelper, RTimer* aTimer, TInt aError);
	void MActiveHelperCallbackDoCancel(CActiveHelper<RTimer*>* aActiveHelper, RTimer* aTimer);
	
	void NotifyConnected();
	
	void SubmitL(CSmrtQuery* aQuery);
	void Cancel(CSmrtQuery* aQuery);
	
	TBool AllRequestsCompleted() const;

	CAknDialog* iSmrtCaptchaDialog;
	
	TBool iCrowdSmrtSession;
	TInt iCrowdSmrtSessionId;
	HBufC8* iSmrtEventValidation;
	HBufC8* iSmrtViewState;
	TBool iSmrtInitalizationCycle;
	CSmrtQuery* iSmrtInitializationPilotService;
	HBufC8* iSmrtSessionCookie;
	TInt iSmrtStatusCode;
	TInt iSmrtCaptcha;
	RHTTPTransaction iSmrtSessionTransaction, iCrowdSmrtSessionTransaction, iSmrtCaptchaTransaction, iSmrtSolveCaptchaTransaction, iCrowdSmrtSessionUpdateTransaction, iSmrtSelectBusTransaction;
	LManagedPtr<CHTTPTransactionTarget> iSmrtSessionTransactionTarget, iCrowdSmrtSessionTransactionTarget, iSmrtSolveCaptchaTransactionTarget, iSmrtCaptchaTransactionTarget, iCrowdSmrtSessionUpdateTransactionTarget, iSmrtSelectBusTransactionTarget;
	void CancelAllSmrtRequestsL();
	void SubmitSmrtSessionCookieRequestL();
	void ReceivedSmrtSessionCookieAnswerL(RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf);
	void SubmitSmrtBusArrivalSearchNoHeaderRequestL(CSmrtQuery* aQuery);
	void ReceivedSmrtBusArrivalSearchNoHeaderAnswerBodyDataL(CSmrtQuery* aQuery, RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf, TInt aPartSize);
	void ReceivedSmrtBusArrivalSearchNoHeaderAnswerL(CSmrtQuery* aQuery, RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf);
	void ReceivedSmrtCaptchaAnswerL(RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf);
	void ReceivedSmrtSolveCaptchaAnswerL(RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf);
	void ReceivedSmrtSelectBusAnswerL(RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf);
	void SmrtCaptchaImageDecoded();
	void SmrtCaptchaImageScaled();
	TInt SmrtTransactionError(CSmrtQuery* aQuery, TInt aError, RHTTPTransaction aTransaction);
	void SubmitCrowdSmrtSessionRequestL();
	void SubmitCrowdSmrtSessionUpdateRequestL();
	void ReceivedCrowdSmrtSessionAnswerL(RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf);
	void ReceivedCrowdSmrtSessionUpdateAnswerL(RHTTPTransaction aTransaction, THTTPEvent aEvent, TInt aLastError, TPtrC8 aBuf);
	TInt CrowdSmrtTransactionError(CSmrtQuery* aQuery, TInt aError, RHTTPTransaction aTransaction);
	
	LManagedPtr<CImageDecoder> iSmrtCaptchaDecoder;
	LManagedPtr<CFbsBitmap> iSmrtCaptchaBitmap;
	LManagedPtr<CBitmapScaler> iSmrtCaptchaBitmapScaler;
	LManagedPtr<CHTTPTransactionSource> iSmrtCaptchaTransactionSource, iSmrtSelectBusTransactionSource, iCrowdSmrtSessionUpdateTransactionSource;
	// targets need to be bottom to be first to be deleted (they trigger cancel 
	LManagedPtr<scalp::CActiveTarget> iSmrtCaptchaDecoderTarget, iSmrtCaptchaBitmapScalerTarget;
	
	};

#endif // __SMRTQUERY__
